<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="refressh" content="5">
<style type='text/css'>
	body{
		background: #EFEFEF;
	}
	body > form > input {
		border-radius:15px;
		background: activecaption;
		color: white;
		height: 40px;
		font: small-caption;
	}

	table {
    border: 1px solid black;
    width: 500px;
	border-radius: 5px;
	margin-top: 10px;
	}
	thead tr {
		background: activecaption;
		color: white;
	}
	tbody tr:nth-child(even){
		background:#F7F7F7;
	}
	tbody tr:nth-child(odd){
		background:#E0E0E0;
	}
	tbody input {
		background: whiteSmoke;
		align:center;
		vertical-align: center;
		font-weight: bold;
		color: activecaption;
		
	}
	form {
		margin: 2px auto;
	}
	
	
</style>
</head>
<body>
<?php
	session_start();
	 
	$userId = $_SESSION['userId'];
	try {
		$db = new PDO("sqlite:siwoz.db");
		$result = $db->query("SELECT name, visit_start, expected_duration, state, a.id as appointment_id FROM appointments a JOIN Patients p on a.patient_id = p.id where physician_id = '$userId' and state <> 2 order by visit_start");
		
		$first_appointment = $db->query("SELECT a.id as appointment_id FROM appointments a JOIN Patients p on a.patient_id = p.id where physician_id = '$userId' and state <> 2 order by visit_start")->fetch();
		$first_appointment_id = $first_appointment['appointment_id'];
		echo "<form method='POST' action='confirm.php' target='_blank'> <input id='appointment_id' type='hidden' name='appointment_id' value='{$first_appointment_id}' /> <input type='submit' value='Przyjmij następną wizytę' /></form>";
		
		echo "<table>";
		echo "<thead>";
		echo "<tr><th>Pacjent</th><th>Data wizyty</th><th>Oczekiwany czas</th><th>Przyjmij</th></tr>";
		echo "</thead>";
		foreach ($result as $row) {
			echo "<tr><td>{$row['name']}</td><td>{$row['visit_start']}</td>";
			echo "<td>{$row['expected_duration']}</td>";
			if ($row['state'] == 1){
				echo "<td>W trakcie</td>";
			} else {
				echo "<td><form method='POST' action='confirm.php' target='_blank'> <input id='appointment_id' type='hidden' name='appointment_id' value='{$row['appointment_id']}' /> <input type='submit' value='Przyjmij' /></form></td>";
			}
			echo "</tr>";
		}
		echo "</table>";
		$db = null;
	}
	catch(PDOException $e)
	{
		echo $e->getMessage();
	}
?>

</body> </html>